<?php

namespace app\api\controller;

use app\api\controller\User as ControllerUser;
use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use app\common\model\User;
use think\Hook;
use think\Db;

class Society extends Api
{
    protected $noNeedLogin = ["getScoietyNotice", "getEducationLists", "applySociety", "htmlToJson", "getEducationLists", "getBusinessTypeLists"];
    protected $noNeedRight = '*';

    //判断我的申请入会状态
    public function getMySocietyStatus()
    {
        $user = $this->auth->getUser();
        
        //获取用户申请信息
        $info = Db::name("society_apply")
        ->where("uid = " . $user->id)
        ->order("createtime desc")
        ->find();

        //1可申请    0不可申请
        if(!$info){
            $this->success("操作成功", ["status" => 1]);
        }

        $this->success("操作成功", ["status" => 0]);
    }

    //获取我的入会信息
    public function getMySocietyDetail()
    {
        $user = $this->auth->getUser();

        //获取用户申请信息
        $info = Db::name("society_apply")
        ->where("uid = " . $user->id)
        ->where("status <> 2")
        ->order("createtime desc")
        ->find();

        if(!$info){
            $this->error("找不到信息");
        }

        $this->success("操作成功", $info);
    }

    //获取入会须知
    public function getScoietyNotice()
    {
        $content = Db::name("single")
        ->where("type_id = 5")
        ->find()["content"] ?? "";

        $this->success("操作成功", $content);

    }

    //申请服务 留学类
    public function applySociety()
    {
        $chamber_type_id = $this->request->post("chamber_type_id", 0); //申请会员类别ID
        $chamber_type_name = $this->request->post("chamber_type_name", ""); //申请会员类别名称
        $name = $this->request->post("name", ""); //姓名
        $name_en = $this->request->post("name_en", ""); //英文姓名
        $sex = $this->request->post("sex", 0); //1男   2女
        $idCard_no = $this->request->post("idCard_no", 0); //身份证号
        $address = $this->request->post("address", ""); //联系地址
        $address_en = $this->request->post("address_en", ""); //英文联系地址
        $mobile = $this->request->post("mobile", ""); //联系方式
        $fax = $this->request->post("fax", ""); //传真
        $email = $this->request->post("email", ""); //邮箱
        $education_id = $this->request->post("education_id", 0);//学历id
        $education_name = $this->request->post("education_name", ""); //学历名称
        $work_years = $this->request->post("work_years", ""); //工作年限
        $function_con = $this->request->post("function_con", ""); // 功能界别 
        $speciality = $this->request->post("speciality", ""); // 专业资格
        $social = $this->request->post("social", ""); //社会服务经验

        $institution_name = $this->request->post("institution_name", ""); //机构名称
        $business_reg_no = $this->request->post("business_reg_no", ""); //商业登记证号
        $business_createtime = $this->request->post("business_createtime", ""); //成立日期
        $position = $this->request->post("position", ""); //职位
        $position_en = $this->request->post("position_en", ""); //职位英文名称
        $business_address = $this->request->post("business_address", ""); //机构联系地址
        $business_address_en = $this->request->post("business_address_en", ""); //机构英文联系地址
        $business_mobile = $this->request->post("business_mobile", ""); //机构联系方式
        $business_fax = $this->request->post("business_fax", ""); //机构传真
        $business_email = $this->request->post("business_email", ""); //机构邮箱
        $business_http = $this->request->post("business_http", ""); //机构网址
        $business_facebook = $this->request->post("business_facebook", ""); //机构facebook
        $business_type_id = $this->request->post("business_type_id", 0); //公司形式id
        $business_type_name = $this->request->post("business_type_name", ""); //公司形式名称
        $business_service = $this->request->post("business_service", ""); //产品/服务
        $business_local_peo_num = $this->request->post("business_local_peo_num", 0); //公司本地人数
        $business_overseas_peo_num = $this->request->post("business_overseas_peo_num", 0); //公司海外人数
        
        $parent_name = $this->request->post("parent_name", ""); //推荐人姓名
        $parent_name_en = $this->request->post("parent_name_en", ""); //推荐人姓名英文
        $parent_no = $this->request->post("parent_no", ""); //推荐人编号
        $parent_chamber_name = $this->request->post("parent_chamber_name", ""); //推荐人在本会职位
        $parent_mobile = $this->request->post("parent_mobile", ""); //推荐人联系方式
        $parent_fax = $this->request->post("parent_fax", ""); //推荐人传真
        $parent_email = $this->request->post("parent_email", ""); //推荐人邮箱

        $user = $this->auth->getUser();

        //插入数据
        Db::name("society_apply")
        ->insert([
            "uid" => $user->id,
            "chamber_type_id" => $chamber_type_id,
            "chamber_type_name" => $chamber_type_name,
            "name" => $name,
            "name_en" => $name_en,
            "sex" => $sex,
            "idCard_no" => $idCard_no,
            "address" => $address,
            "address_en" => $address_en,
            "mobile" => $mobile,
            "fax" => $fax,
            "email" => $email,
            "education_id" => $education_id,
            "education_name" => $education_name,
            "work_years" => $work_years,
            "function_con" => $function_con,
            "speciality" => $speciality,
            "social" => $social,
            "institution_name" => $institution_name,
            "business_reg_no" => $business_reg_no,
            "business_createtime" => $business_createtime,
            "position" => $position,
            "position_en" => $position_en,
            "business_address" => $business_address,
            "business_address_en" => $business_address_en,
            "business_mobile" => $business_mobile,
            "business_fax" => $business_fax,
            "business_email" => $business_email,
            "business_http" => $business_http,
            "business_facebook" => $business_facebook,
            "business_type_id" => $business_type_id,
            "business_type_name" => $business_type_name,
            "business_service" => $business_service,
            "business_local_peo_num" => $business_local_peo_num,
            "business_overseas_peo_num" => $business_overseas_peo_num,
            "parent_name" => $parent_name,
            "parent_name_en" => $parent_name_en,
            "parent_no" => $parent_no,
            "parent_chamber_name" => $parent_chamber_name,
            "parent_mobile" => $parent_mobile,
            "parent_fax" => $parent_fax,
            "parent_email" => $parent_email,
            "createtime" => time()
        ]);

        $this->success("申请成功，请等待审核");
    }

    //html符号转义为json
    public function htmlToJson($string)
    {
        $string = str_replace("&quot;", '"', $string);
        return $string;
    }

    //获取申请会员类别
    public function getChamberLists()
    {
        $lang = $this->request->post("lang", 1);
        if($lang == 1){
            $field = "id, name";
        }else{
            $field = "id, name_hk as name";
        }
        $lists = Db::name("chamber_type")
        ->field($field)
        ->select();

        $this->success("操作成功", $lists);
    }

    //获取最高学历列表
    public function getEducationLists()
    {
        $lang = $this->request->post("lang", 1);
        if($lang == 1){
            $field = "id, name";
        }else{
            $field = "id, name_hk as name";
        }
        $lists = Db::name("education")
        ->field($field)
        ->select();

        $this->success("操作成功", $lists);
    }

    //获取公司形式列表
    public function getBusinessTypeLists()
    {
        $lang = $this->request->post("lang", 1);
        if($lang == 1){
            $field = "id, name";
        }else{
            $field = "id, name_hk as name";
        }
        $lists = Db::name("business_type")
        ->field($field)
        ->select();

        $this->success("操作成功", $lists);
    }

}
